package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldInvoice;
import com.cleaningcloud.cld.domain.CldSettlement;
import com.cleaningcloud.cld.domain.dto.CldSettlementDto;
import com.cleaningcloud.cld.domain.vo.CldSettlementVo;
import com.cleaningcloud.cld.mapper.CldInvoiceMapper;
import com.cleaningcloud.cld.mapper.CldSettlementMapper;
import com.cleaningcloud.cld.service.ICldSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 结算管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-17
 */
@Service
public class CldSettlementServiceImpl implements ICldSettlementService {

    @Autowired
    private CldSettlementMapper cldSettlementMapper;

    @Autowired
    private CldInvoiceMapper cldInvoiceMapper;

    /**
     * 查询结算管理
     *
     * @param settlementId 结算管理主键
     * @return 结算管理
     */
    @Override
    public CldSettlement selectCldSettlementBySettlementId(Long settlementId) {
        return cldSettlementMapper.selectCldSettlementBySettlementId(settlementId);
    }

    /**
     * 查询结算管理列表
     *
     * @param cldSettlement 结算管理
     * @return 结算管理
     */
    @Override
    public List<CldSettlementVo> selectCldSettlementList(CldSettlementDto cldSettlement) {
        return cldSettlementMapper.selectCldSettlementList(cldSettlement);
    }

    @Override
    public List<CldSettlementVo> selectCldSettlementGroupRegionList(CldSettlementDto cldSettlementDto) {
        return cldSettlementMapper.selectCldSettlementGroupRegionList(cldSettlementDto);
    }

    @Override
    public Map<String, Object> selectCldSettlementStatistics(CldSettlementDto cldSettlementDto) {
        return cldSettlementMapper.selectCldSettlementStatistics(cldSettlementDto);
    }

    /**
     * 新增结算管理
     *
     * @param cldSettlement 结算管理
     * @return 结果
     */
    @Override
    public int insertCldSettlement(CldSettlement cldSettlement) {
        return cldSettlementMapper.insertCldSettlement(cldSettlement);
    }

    /**
     * 修改结算管理
     *
     * @param cldSettlement 结算管理
     * @return 结果
     */
    @Override
    public int updateCldSettlement(CldSettlement cldSettlement) {
        //结算收款后更改发票签收状态为已签收
        if(cldSettlement.getCollectionStatus() == 1){
            CldSettlement csl = cldSettlementMapper.selectCldSettlementBySettlementId(cldSettlement.getSettlementId());
            if(null != csl){
                CldInvoice obj = cldInvoiceMapper.selectCldInvoiceByInvoiceId(csl.getInvoiceId());
                if(null != obj){
                    obj.setSignStatus(1L);
                    cldInvoiceMapper.updateCldInvoice(obj);
                }
            }
        }
        // 烂账
        if(cldSettlement.getIsMashed() == 1){
            CldSettlement csl = cldSettlementMapper.selectCldSettlementBySettlementId(cldSettlement.getSettlementId());
            if(null != csl){
                CldInvoice obj = cldInvoiceMapper.selectCldInvoiceByInvoiceId(csl.getInvoiceId());
                if(null != obj){
                    cldSettlement.setBaddebtMoney(obj.getRealInvoiceAmount());
                }
            }
        }else{
            CldSettlement csl = cldSettlementMapper.selectCldSettlementBySettlementId(cldSettlement.getSettlementId());
            if(null != csl){
                CldInvoice obj = cldInvoiceMapper.selectCldInvoiceByInvoiceId(csl.getInvoiceId());
                if(null != obj){
                    cldSettlement.setBaddebtMoney(obj.getRealInvoiceAmount().subtract(cldSettlement.getReceivedMoney()));
                }
            }
        }
        return cldSettlementMapper.updateCldSettlement(cldSettlement);
    }

    /**
     * 批量删除结算管理
     *
     * @param settlementIds 需要删除的结算管理主键
     * @return 结果
     */
    @Override
    public int deleteCldSettlementBySettlementIds(Long[] settlementIds) {
        return cldSettlementMapper.deleteCldSettlementBySettlementIds(settlementIds);
    }

    /**
     * 删除结算管理信息
     *
     * @param settlementId 结算管理主键
     * @return 结果
     */
    @Override
    public int deleteCldSettlementBySettlementId(Long settlementId) {
        return cldSettlementMapper.deleteCldSettlementBySettlementId(settlementId);
    }
}
